package com.zsix.system.strategy.userByTaskRule;

import com.zsix.system.mapper.taskAssigneeMapper.AssigneeUserMapper;
import com.zsix.system.service.IAssigneeUserService;
import com.zsix.common.entity.taskAssignee.AssigneeDSREntity;
import com.zsix.common.entity.taskAssignee.ManuallySelectEntity;
import com.zsix.common.util.RedisUtils;
import com.zsix.common.util.StringUtils;
import com.zsix.common.util.SystemConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 按照手工选择设置节点处理人,多人处理模式
 */
@Component
public class ByManuallySelectList implements UserByTaskRuleStrategy {
    @Autowired
    private IAssigneeUserService assigneeUserService;
    @Autowired
    private AssigneeUserMapper assigneeUserMapper;
    @Autowired
    private RedisUtils redisUtils;
    public HashMap<String,Object> getUserByTaskRule(String taskid, String ruleName
            , HashMap<String,Object> variables, String procInstId) {
        //获取设置的接收人数据
        HashMap<String, Object> hashMap = assigneeUserService
                .getAssigneeUserWithTaskKey(taskid, ruleName);
        List<AssigneeDSREntity> assigneeDSREntityList = (List<AssigneeDSREntity>) hashMap.get("assigneeDSREntities");

        String nextWorkEmp = "";
        List<ManuallySelectEntity> manuallySelectEntities=new ArrayList<>();
        List<String> byManuallySelectList=new ArrayList<>();
        for (AssigneeDSREntity ae : assigneeDSREntityList) {
            String userTaskId="";
            if(!StringUtils.isNull(ae.getUsertaskid()))
                userTaskId=ae.getUsertaskid();
            else
                userTaskId=ae.getId();
            manuallySelectEntities=assigneeUserMapper.getManuallySelectDSR(userTaskId,procInstId);

            if(manuallySelectEntities.size()<=0){
                variables.put("byManuallySelect",ae.getDsr());
                variables.put("nextNode", userTaskId);
            }
            else{
                for (ManuallySelectEntity manuallySelect:manuallySelectEntities){
                    if(nextWorkEmp.contains(manuallySelect.getUsername()+","))
                        continue;
                    byManuallySelectList.add(manuallySelect.getUsername());
                    nextWorkEmp +=  manuallySelect.getUsername() + ",";
                }

            }
        }

        if(!StringUtils.isNull(nextWorkEmp)){
            nextWorkEmp = nextWorkEmp.substring(0, nextWorkEmp.length() - 1);
        }
        //放入缓存，用于发送消息推送
        variables.put(taskid + "_byManuallySelect", byManuallySelectList);
        variables.put(taskid + "_byManuallySelectList", byManuallySelectList);
        if(redisUtils.exists(procInstId+"_sms")){
            redisUtils.remove(procInstId+"_sms");
        }
        redisUtils.set(procInstId+"_sms",nextWorkEmp);
        return variables;
    }
    @Override
    public String getType(){
        return "byManuallySelectList";
    }
}
